package javafx.officialdemo.textfield;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

public class FXTextField {
	/**
	 *  构造器
		Label label1 = new Label("Name:");
		TextField textField = new TextField ();
		HBox hb = new HBox();
		hb.getChildren().addAll(label1, textField);
		hb.setSpacing(10);
	 */
	
	/**
	 * demo
	 */
	
	public void test(){
		//Creating a GridPane container
		GridPane grid = new GridPane();
		grid.setPadding(new Insets(10, 10, 10, 10));
		grid.setVgap(5);
		grid.setHgap(5);

		//Defining the Name text field
		final TextField name = new TextField();
		name.setPromptText("Enter your first name.");
		GridPane.setConstraints(name, 0, 0);
		grid.getChildren().add(name);

		//Defining the Last Name text field
		final TextField lastName = new TextField();
		lastName.setPromptText("Enter your last name.");
		GridPane.setConstraints(lastName, 0, 1);
		grid.getChildren().add(lastName);

		//Defining the Comment text field
		final TextField comment = new TextField();
		comment.setPromptText("Enter your comment.");
		GridPane.setConstraints(comment, 0, 2);
		grid.getChildren().add(comment);

		//Defining the Submit button
		Button submit = new Button("Submit");
		GridPane.setConstraints(submit, 1, 0);
		grid.getChildren().add(submit);

		//Defining the Clear button
		Button clear = new Button("Clear");
		GridPane.setConstraints(clear, 1, 1);
		grid.getChildren().add(clear);

		
		final Label label = new Label();
		GridPane.setConstraints(label, 0, 3);
		GridPane.setColumnSpan(label, 2);
		grid.getChildren().add(label);

		submit.setOnAction(new EventHandler<ActionEvent>() {
			
			public void handle(ActionEvent arg0) {
				if (
				        (comment.getText() != null && !comment.getText().isEmpty())
				    ) {
				    label.setText(name.getText() + " " +
				        lastName.getText() + ", "
				        + "thank you for your comment!");
				    } else {
				        label.setText("You have not left a comment.");
				    }
			}
		});

		clear.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				name.clear();
				lastName.clear();
				comment.clear();
				label.setText(null);
			}
		});
	}
	
	/**
	 * 一些有用的API
	 * 
		copy()– transfers the currently selected range in the text to the clipboard, 
				leaving the current selection.
		
		cut()– transfers the currently selected range in the text to the clipboard, 
			   removing the current selection.
		
		selectAll() - selects all text in the text input.
		
		paste()– transfers the contents in the clipboard into this text,
		 		 replacing the current selection.
	 */
	
	/*------------------------passwordfield--------------------------*/
	/**
		PasswordField passwordField = new PasswordField();
		passwordField.setPromptText("Your password");	
		
		setOnAction添加失焦事件
		getText()获得 密码 字符串。
	 */
	
}
